package com.jblog.common.web.front;

import static com.jblog.Constants.System.PAGE_STRING;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.util.UrlPathHelper;

public final class URLHelper {
    /**
     * 获得页号
     * 
     * @param request
     * @return
     */
    public static int getPageNo(HttpServletRequest request) {
        return getPageNo(getParams(request));
    }

    /**
     * 获得路径信息
     * 
     * @param request
     * @return
     */
    public static String[] getPaths(HttpServletRequest request) {
        return getPaths(getURI(request));
    }

    /**
     * 获得路径参数
     * 
     * @param request
     * @return
     */
    public static String[] getParams(HttpServletRequest request) {
        UrlPathHelper helper = new UrlPathHelper();
        String query = helper.getOriginatingQueryString(request);
        return getParams(query);
    }

    private static String getURI(HttpServletRequest request) {
        UrlPathHelper helper = new UrlPathHelper();
        String uri = helper.getOriginatingRequestUri(request);
        String ctx = helper.getOriginatingContextPath(request);
        if (!StringUtils.isBlank(ctx)) {
            return uri.substring(ctx.length());
        } else {
            return uri;
        }
    }

    /**
     * 获得翻页信息
     * 
     * @param request
     * @return
     */
    public static PageInfo getPageInfo(HttpServletRequest request) {
        UrlPathHelper helper = new UrlPathHelper();
        String uri = helper.getOriginatingRequestUri(request);
        String ctx = helper.getContextPath(request);
        String queryString = helper.getOriginatingQueryString(request);
        return getPageInfo(uri, ctx, queryString);
    }

    /**
     * 获得页号
     * 
     * @param uri
     *            URI {@link HttpServletRequest#getRequestURI()}
     * @return
     */
    public static int getPageNo(String[] params) {
        int pageNo = 1;
        if (params.length > 0) {
            for (String s : params) {
                if (s.startsWith("page")) {
                    try {
                        pageNo = Integer.parseInt(s.split("=")[1]);
                    } catch (NumberFormatException e) {
                        pageNo = 1;
                        break;
                    }
                    break;
                }
            }
        }
        return pageNo;
    }

    /**
     * 获得路径数组
     * 
     * @param uri
     *            URI {@link HttpServletRequest#getRequestURI()}
     * @return
     */
    public static String[] getPaths(String uri) {
        if (uri == null) {
            throw new IllegalArgumentException("URI can not be null");
        }
        if (!uri.startsWith("/")) {
            throw new IllegalArgumentException("URI must start width '/'");
        }
//        int bi = uri.indexOf("_");
//        int mi = uri.indexOf("-");
//        int pi = uri.indexOf(".");
//        // 获得路径信息
//        String pathStr;
//        if (bi != -1) {
//            pathStr = uri.substring(0, bi);
//        } else if (mi != -1) {
//            pathStr = uri.substring(0, mi);
//        } else if (pi != -1) {
//            pathStr = uri.substring(0, pi);
//        } else {
//            pathStr = uri;
//        }
        String[] paths = StringUtils.split(uri, '/');
        return paths;
    }

    /**
     * 获得路径参数
     * 
     * @param uri
     *            URI {@link HttpServletRequest#getRequestURI()}
     * @return
     */
    public static String[] getParams(String queryParam) {

        String[] params;
        if (!StringUtils.isBlank(queryParam)) {
            params = queryParam.split("&");
            
        } else {
            params = new String[0];
        }
        return params;
    }

    /**
     * 获得URL信息
     * 
     * @param uri
     *            URI {@link HttpServletRequest#getRequestURI()}
     * @param queryString
     *            查询字符串 {@link HttpServletRequest#getQueryString()}
     * @return
     */
    public static PageInfo getPageInfo(String uri, String ctx, String queryString) {
        //URL: http://localhost:8080/jblog/category/?status=normal&page=3
        //URI: /jblog/category/
        //CTX: /jblog
        //QUY: status=normal&page=3
        if (uri == null) {
            return null;
        }
        if (!uri.startsWith("/")) {
            throw new IllegalArgumentException("URI must start width '/'");
        }

        String href = uri.substring(ctx.length());
        String[] queryParams = getParams(queryString);
        
        String urlLater = "";
        for (String s : queryParams) {
            if (s.length() > 2 && !s.startsWith(PAGE_STRING)) {
                urlLater += s + "&";
            }
        }
        urlLater += PAGE_STRING + "=";
        
        String urlFormer = "?";
        
        return  new PageInfo(href, urlFormer, urlLater);
    }

    public static class PageInfo {
        private String href;
        private String hrefFormer;
        private String hrefLatter;

        public PageInfo(String href, String hrefFormer, String hrefLatter) {
            this.href = href;
            this.hrefFormer = hrefFormer;
            this.hrefLatter = hrefLatter;
        }

        public String getHref() {
            return href;
        }

        public void setHref(String href) {
            this.href = href;
        }

        public String getHrefFormer() {
            return hrefFormer;
        }

        public void setHrefFormer(String hrefFormer) {
            this.hrefFormer = hrefFormer;
        }

        public String getHrefLatter() {
            return hrefLatter;
        }

        public void setHrefLatter(String hrefLatter) {
            this.hrefLatter = hrefLatter;
        }

    }
}
